// (c) 2015 Teresa Holfeld
package com.holfeld.teresa.vocabtrainer.card;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.holfeld.teresa.vocabtrainer.R;
import com.holfeld.teresa.vocabtrainer.provider.VocabularyPool;

/**
 * @author Teresa Holfeld, teresa.holfeld@gmail.com
 */
public class AddVocableCard extends CardView {

    private String mVocabularyName;

    private String mVocable1;

    private String mVocable2;

    private TextView mVocable1TextView;

    private TextView mVocable2TextView;

    private ButtonCallback mButtonCallback;

    public AddVocableCard(Context context, String vocabularyName) {
        super(context);
        initCallback(context);
        init(vocabularyName);
    }

    public AddVocableCard(Context context, AttributeSet attrs, String vocabularyName) {
        super(context, attrs);
        initCallback(context);
        init(vocabularyName);
    }

    public AddVocableCard(Context context, AttributeSet attrs, int defStyle,
                          String vocabularyName) {
        super(context, attrs, defStyle);
        initCallback(context);
        init(vocabularyName);
    }

    /**
     * Changes the internally held vocabulary name. Does not update the contained vocable.
     *
     * @param newName the new name of the vocabulary
     */
    public void changeVocabularyName(String newName) {
        mVocabularyName = newName;
    }

    /**
     * Sets the focus on the first TextView.
     */
    public void focus() {
        mVocable1TextView.requestFocus();
    }

    /**
     * Checks whether both TextView inputs are empty.
     *
     * @return true if both TextView inputs are empty
     */
    public boolean isEmpty() {
        return TextUtils.isEmpty(mVocable1TextView.getText().toString())
                && TextUtils.isEmpty(mVocable2TextView.getText().toString());
    }

    /**
     * Deletes the AddVocableCard view and the vocable it contains.
     */
    public void delete() {
        VocabularyPool.getInstance(getContext().getApplicationContext())
                .deleteVocable(mVocabularyName, mVocable1, mVocable2);
    }

    private void initCallback(Context context) {
        // Activities containing this fragment must implement its callbacks.
        if (!(context instanceof ButtonCallback)) {
            throw new IllegalStateException("Activity must implement view's callback.");
        }
        mButtonCallback = (ButtonCallback) context;
    }

    private void init(String vocabularyName) {
        inflate(getContext(), R.layout.card_add_vocable, this);
        mVocabularyName = vocabularyName;
        mVocable1TextView = (TextView) findViewById(R.id.vocable1);
        mVocable2TextView = (TextView) findViewById(R.id.vocable2);
        Button saveButton = (Button) findViewById(R.id.save);
        saveButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((View) v.getParent()).setVisibility(GONE);
                saveVocable(mVocable1TextView, mVocable2TextView);
                mButtonCallback.onSaveVocable(AddVocableCard.this);
            }
        });
        Button cancelButton = (Button) findViewById(R.id.cancel);
        cancelButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                VocabularyPool.getInstance(getContext().getApplicationContext())
                        .deleteVocable(mVocabularyName, mVocable1, mVocable2);
                mButtonCallback.onCancelVocable(AddVocableCard.this);
            }
        });
        CardTextWatcher textWatcher = new CardTextWatcher(saveButton, mVocable1TextView,
                mVocable2TextView);
        mVocable1TextView.addTextChangedListener(textWatcher);
        mVocable2TextView.addTextChangedListener(textWatcher);
        saveButton.setEnabled(false);
    }

    private void saveVocable(TextView vocable1Input, TextView vocable2Input) {
        delete();
        mVocable1 = vocable1Input.getText().toString();
        mVocable2 = vocable2Input.getText().toString();
        VocabularyPool.getInstance(getContext().getApplicationContext())
                .addVocable(mVocabularyName, mVocable1, mVocable2);
    }

    /**
     * A callback interface that all activities containing this fragment must
     * implement. This mechanism allows activities to be notified of save actions, and define their
     * own behavior accordingly (e.g. adding another AddVocableCard to the parent layout).
     */
    public interface ButtonCallback {
        public void onSaveVocable(AddVocableCard addVocableCard);
        public void onCancelVocable(AddVocableCard addVocableCard);
    }
}
